﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Ones.Infrastructure.Utils.Pagination.Impl;
using Ones.ServiceAccountManagement;

namespace Ones.SysAdmin
{
    public partial class Accounts : System.Web.UI.Page
    {
        private ServiceAccountManagementClient amc = new ServiceAccountManagementClient("accountBasicHttpBinding");
        private int page = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["page"] != null)
                page = int.Parse(Request.Params["page"]);

            if (Request.Params["keyword"] != null && Request.Params["searchBy"] != null)
            {
                LoadEmployees(Request.Params["searchBy"].ToString(), Request.Params["keyword"].ToString());
            }
            else
            {
                LoadEmployees();
            }
            lockedMsg.Visible = false;
            unlockedMsg.Visible = false;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string searchBy = ddlSearchBy.SelectedValue;
            string keyword = txtSearch.Text;
            Response.Redirect("~/SysAdmin/Accounts.aspx?searchBy=" + searchBy + "&&keyword=" + keyword);
        }

        protected void employeeList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string action = e.CommandName;
            string id = e.CommandArgument.ToString();
            if (action == "detail")
            {
                Session["UserId_gettingDetail"] = id;
                Response.Redirect("./AccountDetail.aspx");
            }
            else if (action == "lock")
            {
                amc.lockUser(id);
                lockedMsg.Visible = true;
                unlockedMsg.Visible = false;
                LoadEmployees();
            }
            else if (action == "unlock")
            {
                amc.unlockUser(id);
                lockedMsg.Visible = false;
                unlockedMsg.Visible = true;
                LoadEmployees();
            }
        }

        private void LoadEmployees()
        {
            //var pagingObj = amc.getEmployeesHaveAccount(page);
            //employeeList.DataSource = pagingObj.Data;
            //employeeList.DataBind();
            //Session["pageInfo"] = pagingObj.TotalPage + ":" + page;

            amc = new ServiceAccountManagementClient("accountBasicHttpBinding");
            var lst = amc.getEmployeesHaveAccount();
            amc.Close();

            int pageNumber = 1;
            if (Request.QueryString["p"] != null)
            {
                pageNumber = int.Parse(Request.QueryString["p"]);
            }
            LazyPagination<ViewMembershipUserEmployee> lstPaged = new LazyPagination<ViewMembershipUserEmployee>(lst.AsQueryable(), pageNumber, 3);

            employeeList.DataSource = lstPaged;
            employeeList.DataBind();

            setPager<ViewMembershipUserEmployee>(lstPaged);
        }

        private void LoadEmployees(string searchBy, string keyWord)
        {
            //var pagingObj = amc.searchEmpHaveAcc(searchBy, keyword, page);
            //employeeList.DataSource = pagingObj.Data;
            //employeeList.DataBind();
            //Session["pageInfo"] = pagingObj.TotalPage + ":" + page;

            amc = new ServiceAccountManagementClient("accountBasicHttpBinding");
            var lst = amc.searchEmpHaveAcc(searchBy, keyWord);
            amc.Close();

            int pageNumber = 1;
            if (Request.QueryString["p"] != null)
            {
                pageNumber = int.Parse(Request.QueryString["p"]);
            }
            LazyPagination<ViewMembershipUserEmployee> lstPaged = new LazyPagination<ViewMembershipUserEmployee>(lst.AsQueryable(), pageNumber, 3);

            employeeList.DataSource = lstPaged;
            employeeList.DataBind();

            setPager<ViewMembershipUserEmployee>(lstPaged);
        }

        private void setPager<T>(LazyPagination<T> lstPaged) where T : class
        {
            pager.PageNumber = lstPaged.PageNumber;
            pager.PageSize = lstPaged.PageSize;
            pager.TotalItems = lstPaged.TotalItems;
            pager.FirstItem = lstPaged.FirstItem;
            pager.LastItem = lstPaged.LastItem;
            pager.HasPreviousPage = lstPaged.HasPreviousPage;
            pager.HasNextPage = lstPaged.HasNextPage;
        }
    }
}